package me.huanmeng.heater.sender;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.base.Joiner;
import lombok.Setter;
import me.huanmeng.heater.exception.ResponseException;
import me.huanmeng.heater.http.KaiheilaApi;
import me.huanmeng.heater.main.HeaterMain;
import me.huanmeng.heater.object.MessageData;
import me.huanmeng.heater.object.UserInfo;

/**
 * 2021/10/4<br>
 * Heater<br>
 *
 * @author huanmeng_qwq
 */
public class User implements CommandSender {
    private UserInfo userInfo;

    public User(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public MessageData sendMessage(String... lines) {
        final KaiheilaApi kaiheilaApi = HeaterMain.getInstance().getKaiheilaApi();
        final JSONObject jsonObject = new JSONObject(kaiheilaApi.doPost("/direct-message/create")
                .form("target_id", getId())
                .form("content", Joiner.on("\n").join(lines)).execute().body());
        if(jsonObject.getInt("code")!=0){
            throw new ResponseException(jsonObject.toString());
        }
        return JSONUtil.toBean(jsonObject.getJSONObject("data"),MessageData.class);
    }

    @Override
    public String getName() {
        return userInfo.getNickname();
    }

    @Override
    public String getFullName() {
        return userInfo.getFullName();
    }

    @Override
    public String getIdentifyNumber() {
        return userInfo.getIdentify_num();
    }

    @Override
    public String getId() {
        return userInfo.getId();
    }
}
